﻿myApp.controller("rpgtController", function ($scope, $http, $modal, $routeParams, authService, $location, $window) {
    document.title = "Penang Realty 世界地產 RPGT Calculator";
    $scope.$emit('newPageLoaded', { 'title': 'Penang Realty 世界地產 RPGT Calculator', 'description': 'Free online RPGT calculator. Try it now.' });

    //$scope.today = function () {
    //    $scope.dt = new Date();
    //};
    //$scope.today();

    //$scope.clear = function () {
    //    $scope.dt = null;
    //};

    //// Disable weekend selection
    //$scope.disabled = function (date, mode) {
    //    return (mode === 'day' && (date.getDay() === 0 || date.getDay() === 6));
    //};

    //$scope.toggleMin = function () {
    //    $scope.minDate = $scope.minDate ? null : new Date();
    //};
    //$scope.toggleMin();

    //$scope.open = function ($event) {
      
    //    $scope.opened = true;
    //    $event.preventDefault();
    //    $event.stopPropagation();
    //};

    //$scope.dateOptions = {
    //    formatYear: 'yy',
    //    startingDay: 1
    //};

    //$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
    //$scope.format = $scope.formats[0];

    //$scope.status = {
    //    opened: false
    //};

    //var tomorrow = new Date();
    //tomorrow.setDate(tomorrow.getDate() + 1);
    //var afterTomorrow = new Date();
    //afterTomorrow.setDate(tomorrow.getDate() + 2);
    //$scope.events =
    //  [
    //    {
    //        date: tomorrow,
    //        status: 'full'
    //    },
    //    {
    //        date: afterTomorrow,
    //        status: 'partially'
    //    }
    //  ];

    //$scope.getDayClass = function (date, mode) {
    //    if (mode === 'day') {
    //        var dayToCheck = new Date(date).setHours(0, 0, 0, 0);

    //        for (var i = 0; i < $scope.events.length; i++) {
    //            var currentDay = new Date($scope.events[i].date).setHours(0, 0, 0, 0);

    //            if (dayToCheck === currentDay) {
    //                return $scope.events[i].status;
    //            }
    //        }
    //    }

    //    return '';
    //};


    $scope.totalExpenses = function () {

        $scope.totalExpensesFee = ($scope.other ? $scope.other : 0) + ($scope.repairFee ? $scope.repairFee : 0) + ($scope.administrationFee ? $scope.administrationFee : 0)
            +( $scope.agentCommission ?  $scope.agentCommission:0) + ($scope.legalFee ? $scope.legalFee:0);

    }

    $scope.calculate = function () {
        validation();
        if ($scope.warning.length > 0)
            return;
        else
            $scope.warning = null
        rpgtRate();
        $scope.exemptionWaiver1 = ($scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0)) * 0.1;
        $scope.rpgt1 = $scope.rpgtRate1 / 100 * ($scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0) - $scope.exemptionWaiver1);
        $scope.rpgt2 = $scope.rpgtRate2 / 100 * ($scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0));
        $scope.rpgt3 = $scope.rpgtRate3 / 100 * ($scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0));
        // Net gain
        $scope.netGain1 = $scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0) - $scope.rpgt1;
        $scope.netGain2 = $scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0) - $scope.rpgt2;
        $scope.netGain3 = $scope.sellingPrice - $scope.propertyPrice - ($scope.totalExpensesFee ? $scope.totalExpensesFee : 0) - $scope.rpgt3;
        $scope.calFinish = true;
    }

    function rpgtRate()
    {
        switch ($scope.holdingYear) {
            case 1:
                $scope.rpgtRate1 = 30;
                $scope.rpgtRate2 = 30;
                $scope.rpgtRate3 = 30;
                break;
            case 2:
                $scope.rpgtRate1 = 30;
                $scope.rpgtRate2 = 30;
                $scope.rpgtRate3 = 30;
                break;
            case 3:
                $scope.rpgtRate1 = 30;
                $scope.rpgtRate2 = 30;
                $scope.rpgtRate3 = 30;
                break;
            case 4:
                $scope.rpgtRate1 = 20;
                $scope.rpgtRate2 = 20;
                $scope.rpgtRate3 = 30;
                break;
            case 5:
                $scope.rpgtRate1 = 15;
                $scope.rpgtRate2 = 15;
                $scope.rpgtRate3 = 30;
                break;

            default:
                $scope.rpgtRate1 = 0;
                $scope.rpgtRate2 = 5;
                $scope.rpgtRate3 = 5;
        }
    }
    $scope.close = function () {

        $scope.warning = null;
        $scope.error = null;
        $scope.info = null;
    }

    function validation()
    {
        $scope.warning = [];
        if (!$scope.sellingPrice)
            $scope.warning.push({ msg: "Selling price must not be empty!" })
        if (!$scope.propertyPrice)
            $scope.warning.push({ msg: "Purchase price must not be empty!" })
        if (!$scope.holdingYear)
            $scope.warning.push({ msg: "Holding year must not be empty!" })

    }

})